<template>
  <div class="box">
    <el-breadcrumb class="app-breadcrumb" separator="/">
      <transition-group name="breadcrumb">
        <el-breadcrumb-item
          v-for="(item, index) in levelList"
          :key="item.path"
          :to="{ path: item.path }"
        >
          <span style="font-weight: 600; color: #000">{{
            item.meta.title
          }}</span>
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
  </div>
</template>

<script>
export default {
  data() {
    return {
      levelList: [],
    };
  },
  watch: {
    $route() {
      this.getBreadcrumb();
    },
  },
  created() {
    this.getBreadcrumb();
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.filter(
        (item) => item.meta && item.meta.title
      );
      const first = matched[0];
      if (!this.isIndex(first)) {
        // matched = [{ path: '/Home', meta: { title: '首页' } }].concat(matched)
        this.levelList = matched;
      } else {
        // this.levelList = [{ path: '/Home', meta: { title: '首页' } }]
      }
    },
    isIndex(route) {
      const redirect = route && route.redirect;
      if (!redirect) {
        return false;
      }
      // return redirect === '/Home'
    },
  },
};
</script>

<style lang="scss">
/* breadcrumb transition */
.box {
  height: 20px;
  padding: 4px 0px 0px 9px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ccc;
  background-color: #fff;
  margin-bottom: 5px;
}
.app-breadcrumb {
  margin: 0;
}
</style>
